/**
 * @author attilax 老哇的爪子
	@since  2014年5月11日 下午5:10:51$
	
	com.attilax.qrcode.qrcodex
 */
package com.attilax.qrcode;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

import javax.imageio.ImageIO;

import com.attilax.util.tryX;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.datamatrix.DataMatrixReader;

/**
 * @author attilax 老哇的爪子
 * @since 2014年5月11日 下午5:10:51$
 */
public class qrcodex {
	
	/**
	 * @author attilax 老哇的爪子
	 * @since 2014年5月11日 下午5:10:51$
	 * 
	 * @param args
	 * @throws IOException
	 * @throws NotFoundException
	 */
	public static void main(String[] args) throws NotFoundException, IOException {
		// attilax 老哇的爪子 下午5:10:51 2014年5月11日
		// gene("aa","100","100","c:\\qrcode.png");
		String r = null;
		  long starTime=System.currentTimeMillis();
	 r = t_md(r);
	 	//r=t_qr();
		long end = System.currentTimeMillis() - starTime; // 这段代码放在程序执行后
		System.out.println("耗时：" + end + "毫秒");
		
		System.out.println("===r:" + r);
	

		// gene(content, path, width, height);

		System.out.println("ok");
	
		
	}

	private static String t_qr() {
		String imagePath = "C:\\00\\qrcode.jpg";
		
		 
		String r = null;
		try {
			r = decode(imagePath);
		} catch (NotFoundException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		return r;
	}

	private static String t_md(String r) throws NotFoundException, IOException {
		final String content = "aa餐巾纸a";
		final String path = "C:/餐巾纸.jpg";

		final int width = 400;
		final int height = 401;

		String imagePath = "C:\\00\\a2.jpg";
	
		try {
			r = decode_MD(imagePath);
		} catch (ChecksumException | FormatException e) {
			 
			e.printStackTrace();
		}
		return r;
	}

	/**
	 * @author attilax 老哇的爪子
	 * @since 2014年5月11日 下午5:10:51$
	 */
	public qrcodex() {
		// TODO Auto-generated constructor stub
	}

	public static String decode(String imagePath) throws NotFoundException, IOException {
		String contents = null;

		MultiFormatReader formatReader = new MultiFormatReader();

		BufferedImage image;

		image = ImageIO.read(new File(imagePath));

		// 将图像数据转换为1 bit data
		LuminanceSource source = new BufferedImageLuminanceSource(image);
		Binarizer binarizer = new HybridBinarizer(source);
		// BinaryBitmap是ZXing用来表示1 bit data位图的类，Reader对象将对它进行解析
		BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);

		Map hints = new HashMap();
		hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");

		// 对图像进行解码
		Result result = formatReader.decode(binaryBitmap, hints);
		contents = result.toString();

		System.out.println("barcode encoding format :\t " + result.getBarcodeFormat());

		return contents;
	}

	public static String decode_MD(String imagePath) throws NotFoundException, IOException, ChecksumException, FormatException {
		String contents = null;

	 	DataMatrixReader formatReader = new DataMatrixReader();
	//	MultiFormatReader formatReader = new MultiFormatReader();
		BufferedImage image;

		image = ImageIO.read(new File(imagePath));

		// 将图像数据转换为1 bit data
		LuminanceSource source = new BufferedImageLuminanceSource(image);
		Binarizer binarizer = new HybridBinarizer(source);
		// BinaryBitmap是ZXing用来表示1 bit data位图的类，Reader对象将对它进行解析
		BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);

		Map hints = new HashMap();
 
		
		
		//	decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS);
		 Vector<BarcodeFormat> decodeFormats = new Vector<BarcodeFormat>();
	  //      decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS);
	     //   decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS);
	        decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS);
	  //      hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
	        
	        
	    //    hints.put(DecodeHintType.CHARACTER_SET, "ISO88591");
	//		hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
	 
		
		// 对图像进行解码
		Result result = formatReader.decode(binaryBitmap, hints);
		contents = result.toString();

		System.out.println("barcode encoding format :\t " + result.getBarcodeFormat());

		return contents;
	}

	// attilax 老哇的爪子 下午5:10:51 2014年5月11日

	

	/**
	 * 
	 @author attilax 老哇的爪子
	 * @since 2014年5月11日 下午5:35:54$
	 * 
	 * @param content
	 * @param path
	 * @param width
	 * @param height
	 */
	public static void gene(final String content, final String path, final int width, final int height) {
		new tryX<Object>() {

			@Override
			public Object item(Object t) throws Exception {
				// attilax 老哇的爪子 下午5:23:26 2014年5月11日
				MultiFormatWriter multiFormatWriter = new MultiFormatWriter();

				Map hints = new HashMap();
				hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

				BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
				File file1 = new File(path);
				MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
				return null;
			}
		}.$("");
	}

	@Deprecated
	private static void gene(String code, String strHeight, String strWidth, String file) {
		// OutputStream out
		OutputStream output = null;
		try {
			output = new FileOutputStream(new File(file));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} // OutputStream out = new FileOutputStream(f);

		try {
			if (strHeight != null && !"".equals(strHeight) && strWidth != null && !"".equals(strWidth)) {
				BarCodeUtil.makeBarCoder(code, Integer.parseInt(strHeight), Integer.parseInt(strWidth), output);
			} else {
				BarCodeUtil.makeBarCoder(code, output);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			output.flush();
			output.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

// attilax 老哇的爪子